


import org.springframework.web.servlet.ModelAndView
import org.authsum.stitches.*
import org.authsum.stitches.external.*

class ContentController {
	def stitchesService

	def createContentForm = {

		def contentRequest = new ContentRequest()
		def contentHolder = new ContentHolder()
		contentRequest.contentHolder = contentHolder
		contentRequest.contentHolder.contentDefId = params['id'].toLong()
		return new ModelAndView("/content/contentForm",  [contentHolder:contentHolder])
		
	}
	
	
	def editContent = {
			
		def contentRequest = new ContentRequest()
		//def contentHolder = new ContentHolder()
		
		
		//need to populate the content
		contentRequest.contentId = params['id'].toLong()
		def contentResponse =stitchesService.findContentById(contentRequest)
		def contentHolder = contentResponse.contentHolders.contentHolder.get(0)
		
		
		return new ModelAndView("/content/contentForm",  [contentHolder:contentHolder])
	}
	
	
	def index = { 
			
		def contentRequest = new ContentRequest()
		
		contentRequest.contentDefId = params['id'].toLong()
		def contentResponse =stitchesService.findContents(contentRequest)
		
		//assert contentResponse.responseCode == '200'
		[contentResponse:contentResponse,contentDefId:contentRequest.contentDefId ]
		
		
		
	}
	
	
	
	
	def saveContent = {
		
			
		def contentRequest = new ContentRequest()
		def contentHolder = new ContentHolder()		
		bindData(contentHolder, params)

		
		contentRequest.contentHolder = contentHolder
		
		def contentResponse =stitchesService.saveContent(contentRequest)	 
		
		contentResponse.getMessages()?.string.each {
			println it
			flash.message += it
		}
		
		assert contentResponse.responseCode == '200'
		
		return new ModelAndView("/content/contentForm",  [contentHolder:contentHolder])
		
	}
}
